package com.chenyun.common.context;

import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

/**
 *
 * @author: Lonni
 * @date: 2022/5/27 0027 14:28
 */
public class ServletContext {
    public static HttpServletRequest getRequest() {

        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }

    public static String getParameter(String paraName) {
        return getRequest().getParameter(paraName);
    }

    public static Enumeration<String> getParameterNames() {
        return getRequest().getParameterNames();
    }
    public static Map<String, String[]> getParameterMap() {
        return getRequest().getParameterMap();
    }

    /**
     * 设置request实例共享,传递给子线程
     */
    public static void setRequestToThreadShard() {
        //设置主线程
        ServletRequestAttributes sra = getServletRequestAttributes();
        //设置子线程共享
        RequestContextHolder.setRequestAttributes(sra, true);
    }
}
